Imports System
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Namespace DNNQuickForm
    Partial Public Class FormBuilderOptions
        Inherits DotNetNuke.Entities.Modules.ModuleSettingsBase
        Public Overrides Sub LoadSettings()
            Try
                If Not Page.IsPostBack Then
                    ' Load settings
                    BindForms()
                    Dim settings As Hashtable = PortalSettings.GetModuleSettings(ModuleId)
                    If Not CType(settings("DNNQF_FormName"), String) Is Nothing Then
                        drpForms.SelectedIndex = drpForms.Items.IndexOf(drpForms.Items.FindByValue(CType(settings("DNNQF_FormName"), String)))
                    End If
                    If Not CType(settings("DNNQF_To"), String) Is Nothing Then
                        txtTo.Text = CType(settings("DNNQF_To"), String)
                    End If
                    If Not CType(settings("DNNQF_CC"), String) Is Nothing Then
                        txtCC.Text = CType(settings("DNNQF_CC"), String)
                    End If
                    If Not CType(settings("DNNQF_BCC"), String) Is Nothing Then
                        txtBCC.Text = CType(settings("DNNQF_BCC"), String)
                    End If
                    If Not CType(settings("DNNQF_Header"), String) Is Nothing Then
                        txtHeader.Text = CType(settings("DNNQF_Header"), String)
                    End If
                    If Not CType(settings("DNNQF_Footer"), String) Is Nothing Then
                        txtFooter.Text = CType(settings("DNNQF_Footer"), String)
                    End If
                    If Not CType(settings("DNNQF_AddIPAddress"), String) Is Nothing Then
                        chkAddIPAddress.Checked = CType(settings("DNNQF_AddIPAddress"), Boolean)
                    End If
                    If Not CType(settings("DNNQF_AddUserAgent"), String) Is Nothing Then
                        chkAddUserAgent.Checked = CType(settings("DNNQF_AddUserAgent"), Boolean)
                    End If
                    If Not CType(settings("DNNQF_AddTimeStamp"), String) Is Nothing Then
                        chkAddTimestamp.Checked = CType(settings("DNNQF_AddTimeStamp"), Boolean)
                    End If
                    If Not CType(settings("DNNQF_Subject"), String) Is Nothing Then
                        txtSubject.Text = CType(settings("DNNQF_Subject"), String)
                    End If
                    If Not CType(settings("DNNQF_Redirect"), String) Is Nothing Then
                        txtRedirect.Text = CType(settings("DNNQF_Redirect"), String)
                    End If
                    If Not CType(settings("DNNQF_Thankyou"), String) Is Nothing Then
                        txtThankyou.Text = CType(settings("DNNQF_Thankyou"), String)
                    End If
                    If Not CType(settings("DNNQF_SubmitText"), String) Is Nothing Then
                        txtSubmitText.Text = CType(settings("DNNQF_SubmitText"), String)
                    End If
                    If Not CType(settings("DNNQF_SubmitCSSClass"), String) Is Nothing Then
                        txtSubmitCSSClass.Text = CType(settings("DNNQF_SubmitCSSClass"), String)
                    End If



                End If
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Public Overrides Sub UpdateSettings()
            Dim objModules As New Entities.Modules.ModuleController
            ' Update Settings
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_FormName", drpForms.SelectedItem.Value)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_To", txtTo.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_CC", txtCC.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_BCC", txtBCC.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_Header", txtHeader.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_Footer", txtFooter.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_AddIPAddress", chkAddIPAddress.Checked)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_AddUserAgent", chkAddUserAgent.Checked)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_AddTimeStamp", chkAddTimestamp.Checked)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_Subject", txtSubject.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_Redirect", txtRedirect.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_Thankyou", txtThankyou.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_SubmitText", txtSubmitText.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNQF_SubmitCSSClass", txtSubmitCSSClass.Text)
        End Sub
        Private Function GetString(key As String, defaultValue As String) As String
            If (Settings.ContainsKey(key)) Then
                If Not String.IsNullOrEmpty(Settings(key).ToString()) Then
                    Return Settings(key).ToString()
                End If
            End If
            Return defaultValue
        End Function

        Private Sub BindForms()
            Dim strPath As String
            strPath = Request.MapPath(PortalSettings.HomeDirectory) & "DNNQuickForms"
            If Not (Directory.Exists(strPath)) Then
                Directory.CreateDirectory(strPath)
            End If
            drpForms.Items.Add(New ListItem("Default", String.Empty))
            Dim dirInfo As New DirectoryInfo(strPath)
            drpForms.DataTextField = "Name"
            drpForms.DataValueField = "Name"
            drpForms.DataSource = dirInfo.GetFiles("*.ascx")
            drpForms.DataBind()
        End Sub
    End Class
End Namespace